using Microsoft.AspNetCore.Mvc;    //1111
using MvcSoft1Demo.Models;      //1111
using System.Linq;          //1111
using MvcSoft1Demo.Repository;       //2222
using MvcSoft1Demo.RequestParams;
 
namespace MvcSoft1Demo.Controllers          //1111
{                                            //1111
    public class StudentController : Controller     //1111
    {                                               //1111
        public IActionResult Index()                    //1111
        {                                                 
            var schoolRep = new EfRepository<School>();      
            var school = schoolRep.Table.FirstOrDefault();   
            if (school == null)
            {
                schoolRep.Insert(new School
                {
                    SchoolName = "闽大"
                });
            }
            var studentRep = new EfRepository<Student>();    //2222
            var students = studentRep.Table.Where(x => x.IsDeleted == false).ToList();      //2222
            return View(students);        ////1111无students
        }                                       //2222加入students

        public IActionResult CreateOrEdit()    //4444
        {                                   //4444
            return View();                      //4444
        }                                           //4444

        public IActionResult Save(StudentParam request)   //5555无括号内容
        {
            var studentRep = new EfRepository<Student>();
            if (request.Id > 0)
            {

            }
            else
            {
                studentRep.Insert(
                    new Student
                    {
                        StudentName = request.Name.Trim(),
                        Remarks = request.Remarks.Trim(),
                        SchoolId = 1
                    }
                );
            }
            return Ok();        //5555还是view
        }

        public IActionResult Query(QueryParam request)   
        {
            var studentRep = new EfRepository<Student>();
            var keyword = request.Keyword != null ? request.Keyword.Trim() : "";
            var students = studentRep.Table.Where(x => x.IsDeleted == false);

            if (keyword.Length > 0)
            {
                students=students.Where(x=>x.StudentName.Contains(keyword));
            }
            return Json(students.ToList());  
        }
    }
}